
//日期联动
function datePicker(type) {
	WdatePicker({
		skin : 'whyGreen',
		onpicked : function(dp) {
			var d = $dp.$DV($dp.cal.getNewDateStr(),
					{M:1}
			);
			var dd;
			if (d.M < 10) {
				if (d.d < 10) {
					dd = d.y + '-0' + d.M + '-0' + d.d;
				} else {
					dd = d.y + '-0' + d.M + '-' + d.d;
				}
			} else {
				if (d.d < 10) {
					dd = d.y + '-' + d.M + '-0' + d.d;
				} else {
					dd = d.y + '-' + d.M + '-' + d.d;
				}
			}
			$dp.$('endTime').value = dd;
			return true;
		}
	});
}

//日期联动
function datePicker() {
	WdatePicker({
		skin : 'whyGreen',
		onpicked : function(dp) {
			var d = $dp.$DV($dp.cal.getNewDateStr(),
					{M:1}
			);
			var dd;
			if (d.M < 10) {
				if (d.d < 10) {
					dd = d.y + '-0' + d.M + '-0' + d.d;
				} else {
					dd = d.y + '-0' + d.M + '-' + d.d;
				}
			} else {
				if (d.d < 10) {
					dd = d.y + '-' + d.M + '-0' + d.d;
				} else {
					dd = d.y + '-' + d.M + '-' + d.d;
				}
			}
			$dp.$('endTime').value = dd;
			return true;
		}
	});
}

function getDataById(gridName,id) {
	var result = "";
	var data = $('#'+gridName).myTable('getDatas');
	$.each(data.rows, function(index, item) {
		if (item.id == id) {
			result = item;
		}
	});
	return result;
}

//去掉html标签
function escapeSpecialCharacters(str) {
	str = str.replace(/[ | ]*\n/g, '\n'); //去除行尾空白
	str = str.replace(/\n[\s| | ]*\r/g, '\n'); //去除多余空行
	str = str.replace(/&nbsp;/ig, ''); //去掉&nbsp;
	str = str.replace(/\\/g, "\\");
	str = str.replace(/\//g, "\/");
	str = str.replace(/</g, "&lt;");
	str = str.replace(/>/g, "&gt;");
	str = str.replace(/"/g, "&quot;");
	str = str.replace(/'/g, "\'");
	return str;
}
String.prototype.getCharLen = function() {
	var l = 0;
	var a = this.split("");
	for (var i = 0; i < a.length; i++) {
		if (a[i].charCodeAt(0) < 299) {
			l++;
		} else {
			l += 2;
		}
	}
	return l;
};

//获取当前视口的中间y坐标高度   px偏移量
function getViewCenterYPosition(px) {
	var sTop = 0;
	var ppx = 160;
	//页面在iframe内面打开
	if (typeof (px) != "undefined" && !isNaN(px)) {
		ppx = px;
	}
	if (self.frameElement && self.frameElement.tagName == "IFRAME") {
		var top = ($(window.parent).height() - ppx - 100) / 2;
		var scrollTop = window.parent.document.documentElement.scrollTop;
		sTop = scrollTop + top;
	} else { //页面在iframe外面打开
		var top = ($(window).height() - ppx) / 2;
		var mainScrollTop = $(document).scrollTop();
		sTop = mainScrollTop + top;
	}
	//设置遮罩的高度 高于easyui表格350px
	if ($('.datagrid-view').css('height')) {
		var tableHeight = parseFloat($('.datagrid-view').css('height').split(
				'px')[0]);
		if (tableHeight < 1000) {
			tableHeight = 1000;
		}
		$('.window-mask').css('height', tableHeight + 350);
	}
	if (sTop < 5) {
		sTop = 5;
	}
	return parseInt(sTop);
}

String.prototype.getDBUTF8Len = function() {
	var l = 0;
	var a = this.split("");
	for (var i = 0; i < a.length; i++) {
		if (a[i].charCodeAt(0) < 299) {
			l++;
		} else {
			l += 3;
		}
	}
	return l;
};

String.prototype.getSubstr = function(len) {
	var str = this;
	if (!str || !len) {
		return '';
	}
	// 预期计数：中文2字节，英文1字节
	var a = 0;
	// 循环计数
	var i = 0;

	// 临时字串
	var temp = '';
	for (i = 0; i < str.length; i++) {
		if (str.charCodeAt(i) > 255) {
			// 按照预期计数增加2
			a += 2;
		} else {
			a++;
		}
		// 如果增加计数后长度大于限定长度，就直接返回临时字符串
		if (a > len) {
			return temp;
		}
		// 将当前内容加到临时字符串
		temp += str.charAt(i);
	}
	// 如果全部是单字节字符，就直接返回源字符串
	return str;
};

String.prototype.getDBUTF8Substr = function(len) {
	var str = this;
	if (!str || !len) {
		return '';
	}
	// 预期计数：中文3字节，英文1字节
	var a = 0;
	// 循环计数
	var i = 0;

	// 临时字串
	var temp = '';
	for (i = 0; i < str.length; i++) {
		if (str.charCodeAt(i) > 255) {
			// 按照预期计数增加2
			a += 3;
		} else {
			a++;
		}
		// 如果增加计数后长度大于限定长度，就直接返回临时字符串
		if (a > len) {
			return temp;
		}
		// 将当前内容加到临时字符串
		temp += str.charAt(i);
	}
	// 如果全部是单字节字符，就直接返回源字符串
	return str;
};

// 输入框的长度限制方法
function checkMaxLength(obj, len) {
	var strValue = obj.value;
	if (strValue.getDBUTF8Len() >= len) {
		obj.value = strValue.getDBUTF8Substr(len);
	}
}

// 验证是否是小数或者整数或者负数
function isDecimal(obj, whole, dec, isNeg) {
	var strValue = obj.value;
	var negFlag = "";
	if (isNeg) {
		if (strValue.indexOf("-") != -1 && strValue.indexOf("-") == 0) {
			negFlag = "-";
			strValue = strValue.substring(1);
		}
	}
	if (!/^(\d)*(\.(\d)*)?$/.exec(strValue)) {
		obj.value = "";
	}
	if (strValue.length <= whole + dec + 1) {
		if (strValue.indexOf(".") > -1) {
			if (strValue.indexOf(".") > parseInt(whole)) {
				obj.value = negFlag + strValue.getDBUTF8Substr(whole);
			} else {
				if (strValue.length - strValue.indexOf(".") - 1 > dec) {
					obj.value = negFlag
							+ strValue.getDBUTF8Substr(strValue.indexOf(".")
									+ 1 + dec);
				}
			}
		} else {
			if (strValue.length > parseInt(whole)) {
				obj.value = negFlag + strValue.getDBUTF8Substr(whole);
			}
		}
	} else {
		if (strValue.indexOf(".") > -1) {
			if (strValue.indexOf(".") > parseInt(whole)) {
				obj.value = negFlag + strValue.getDBUTF8Substr(whole);
			} else {
				if (strValue.length - strValue.indexOf(".") - 1 > dec) {
					obj.value = negFlag
							+ strValue.getDBUTF8Substr(strValue.indexOf(".")
									+ 1 + dec);
				}
			}
		} else {
			obj.value = negFlag + strValue.getDBUTF8Substr(whole + dec + 1);
		}
	}
}

// 验证是否为数字 可以含小数点
// 否就清空
function checkNumber(obj) {
	if (isNaN(obj.value)) {
		obj.value = "";
	}
}

// 验证是否为数字 不含小数点 0可以是第一位
// 把不是数字的去掉
function checkData(obj) {
	obj.value = obj.value.replace(/\D/g, '');
}

// 验证一个数字是否为整数（正、负、正负）
// 整数的长度
function checkIntegerUtil(value, posOrNeg, startWhole, endWhole) {
	var re;
	if (parseInt(startWhole) > 0) {
		startWhole = parseInt(startWhole) - 1;
	} else {
		startWhole = 1;
	}
	if (parseInt(endWhole) > 0) {
		endWhole = parseInt(endWhole) - 1;
	} else {
		endWhole = 1;
	}
	if (posOrNeg == 1 || posOrNeg == '1') {
		re = new RegExp("^[+]?[0-9]\\d{" + startWhole + "," + endWhole + "}$");
	} else if (posOrNeg == 2 || posOrNeg == '2') {
		re = new RegExp("^[-]?[0-9]\\d{" + startWhole + "," + endWhole + "}$");
	} else {
		re = new RegExp("^[+-]?[0-9]\\d{" + startWhole + "," + endWhole + "}$");
	}
	dd = (re.test(value));
	return dd;

}

// 验证是否为正整数
// obj：数字 posOrNeg：1正整数 2：负整数 0：不要求
function checkInteger(obj, posOrNeg, startWhole, endWhole) {
	var posNeg;
	if (posOrNeg == 1 || posOrNeg == '1') {
		posNeg = /^[+]?]*$/;
	} else if (posOrNeg == 2 || posOrNeg == '2') {
		posNeg = /^[-]?]*$/;
	} else {
		posNeg = /^[+-]?]*$/;
	}
	if (!checkIntegerUtil(obj.value, posOrNeg, startWhole, endWhole)
			&& !posNeg.test(obj.value)) {
		obj.value = "";
		obj.focus();
		return false;
	}
}

// 验证是否为正整数 可是为0
// obj：数字 posOrNeg：1正整数 2：负整数 0：不要求
function checkIntegerHasZero(obj, posOrNeg, startWhole, endWhole) {
	var posNeg;
	if (posOrNeg == 1 || posOrNeg == '1') {
		posNeg = /^[+]?]*$/;
	} else if (posOrNeg == 2 || posOrNeg == '2') {
		posNeg = /^[-]?]*$/;
	} else {
		posNeg = /^[+-]?]*$/;
	}
	if (!checkIntegerUtil(obj.value, posOrNeg, startWhole, endWhole)
			&& !posNeg.test(obj.value) && obj.value != 0) {
		obj.value = "";
		obj.focus();
		return false;
	}
}

// 是否是小数
// obj dec小数位
function checkDecimal(obj, posOrNeg, startWhole, endWhole, startDec, endDec) {
	var re;
	var posNeg;
	if (posOrNeg == 1 || posOrNeg == '1') {
		re = new RegExp("^[+]?\\d{" + startWhole + "," + endWhole + "}(\\.\\d{"
				+ startDec + "," + endDec + "})?$");
		posNeg = /^[+]?]*$/;
	} else if (posOrNeg == 2 || posOrNeg == '2') {
		re = new RegExp("^[-]?\\d{" + startWhole + "," + endWhole + "}(\\.\\d{"
				+ startDec + "," + endDec + "})?$");
		posNeg = /^[-]?]*$/;
	} else {
		re = new RegExp("^[+-]?\\d{" + startWhole + "," + endWhole
				+ "}(\\.\\d{" + startDec + "," + endDec + "})?$");
		posNeg = /^[+-]?]*$/;
	}
	if (!re.test(obj.value) && !posNeg.test(obj.value)) {
		obj.value = "";
		obj.focus();
		return false;
	}
}

// 是否是小数
// obj
function checkPlusMinus(obj) {
	posNeg1 = /^[+]?]*$/;
	posNeg2 = /^[-]?]*$/;
	if (posNeg1.test(obj.value) || posNeg2.test(obj.value)) {
		obj.value = "";
		//obj.focus();
		return false;
	}
}
// 验证文件的大小
function checkFileSize(obj, size) {
	if (obj.val().length == 0)
		return true;
	var Sys = {};
	if (navigator.userAgent.indexOf("MSIE") > 0) {
		Sys.ie = true;
	}
	if (isFirefox = navigator.userAgent.indexOf("Firefox") > 0) {
		Sys.firefox = true;
	}
	var filesize = 0;
	// 火狐下
	if (Sys.firefox) {
		filesize = obj[0].files[0].fileSize;
	} else if (Sys.ie) {
		var filePath = obj[0].value;
		var image = new Image();
		image.src = filePath;
		filesize = image.fileSize;
	}
	// 如果文件大小超过
	if (filesize > size * 1024 * 1024) {
		alert("附件超过" + size + "M");
		obj.focus();
		return false;
	}
	return true;
}

// 验证上传图片格式问题
function checkFileType(obj) {
	if (obj.val().length == 0)
		return true;
	// 判断图片格式
	var type = obj.val().substr(obj.val().lastIndexOf(".") + 1,
			obj.val().length);
	type = type.toUpperCase();
	if (type == "JPG" || type == "PNG" || type == "GIF" || type == "BMP"
			|| type == "JPEG") {
	} else {
		obj.focus();
		alert("图片格式不正确");
		return false;
	}
	return true;
}

//验证上传图片格式问题
function checkFileTypeExcel(obj) {
	if (obj.val().length == 0)
		return true;
	// 判断图片格式
	var type = obj.val().substr(obj.val().lastIndexOf(".") + 1,
			obj.val().length);
	type = type.toUpperCase();
	if (type == "XLSX" || type == "XLS") {
	} else {
		obj.focus();
		alert("文件格式不正确");
		return false;
	}
	return true;
}

/**
 * 验证数据是否为空
 * @param value
 */
function isEmpty(value) {
	if (value == null || value == undefined
			|| value.toString().replace(/(^\s*)|(\s*$)/g, '').length == 0) {
		return true;
	}
	return false;
}

/**
 * 验证数据是否为数值
 */
function isNum(value) {
	if (!isEmpty(value) && !isNaN(value)) {
		return true;
	}
	return false;
}

/**
 * 为数组对象添加index方法
 */
(function() {
	if (!Array.indexof) {
		/**
		 * 获取值在数组对象中的位置
		 */
		Array.prototype.indexof = function(value) {
			for (var i = 0; i < this.length; i++) {
				if (this[i] == value) {
					return i;
				}
			}
			return -1;
		};
	}
})();

//去掉html标签
function removeHtmlTag(str) {
	str = str.replace(/<\/?[^>]*>/g, ''); //去除HTML tag
	str = str.replace(/[ | ]*\n/g, '\n'); //去除行尾空白
	str = str.replace(/\n[\s| | ]*\r/g, '\n'); //去除多余空行
	str = str.replace(/&nbsp;/ig, ''); //去掉&nbsp;
	str = str.replace(/\\/g, "");
	str = str.replace(/</g, "");
	return str;
}

//验证URL
function validateUrl(str) {
	var patten = /^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i;
	return patten.test(str);
}

//获取当前页面宽度的百分比
function getWidth(percent) {
	return document.body.clientWidth * percent;
}

/**
 * 计算时间差
 * @param dtStart
 * @param dtEnd
 * @returns
 */
function dateDiff(sDate1, sDate2) {

	//  if (typeof dtStart == 'string' )//如果是字符串转换为日期型    
	//  {     
	//  	dtStart = StringToDate(dtStart);    
	//  }    
	//  if (typeof dtEnd == 'string' )//如果是字符串转换为日期型    
	//  {     
	//      dtEnd = StringToDate(dtEnd);    
	//  }    
	//  return parseInt((dtEnd - dtStart) / 86400000);
	//sDate1和sDate2是2002-12-18格式      
	var aDate, oDate1, oDate2, iDays;
	aDate = sDate1.split("-");
	oDate1 = new Date(aDate[0], aDate[1] - 1, aDate[2]);
	aDate = sDate2.split("-");
	oDate2 = new Date(aDate[0], aDate[1] - 1, aDate[2]);

	iDays = parseInt((oDate2 - oDate1) / 1000 / 60 / 60 / 24);
	//  if((oDate1 - oDate2)<0){    
	//      return -iDays;    
	//  }
	return iDays;
}
/**
 * 日期转换成字符串
 * @param dateTime
 * @returns {String}
 */
function dateTimeToLocalString(dateTime) {
	if (dateTime == null) {
		return "";
	}
	//	var date = new Date(dateTime);
	//	return date.format("yyyy-MM-dd hh:mm");
	var date = new Date(dateTime);
	Y = date.getFullYear();
	M = (date.getMonth() + 1) < 10 ? ("0" + (date.getMonth() + 1)) : (date
			.getMonth() + 1);
	D = date.getDate() < 10 ? ("0" + date.getDate()) : date.getDate();
	var hh = date.getHours() < 10 ? ("0" + date.getHours()) : date.getHours();
	var mm = date.getMinutes() < 10 ? ("0" + date.getMinutes()) : date
			.getMinutes();
	var ss = date.getSeconds() < 10 ? ("0" + date.getSeconds()) : date
			.getSeconds();
	return Y + '-' + M + '-' + D + ' ' + hh + ':' + mm + ':' + ss;
}
//获取当前页面宽度的百分比
function getWidth(percent) {
	return document.body.clientWidth * percent;
}

/** 返回接口测试页面函数 */
function apiTestGoBack() {
	window.location.href = "../apiTest/toIndexPage.action";
}

function serializeData(data) {

	// If this is not an object, defer to native stringification.
	if (!angular.isObject(data)) {

		return ((data == null) ? "" : data.toString());

	}

	var buffer = [];

	// Serialize each key in the object.
	for ( var name in data) {

		if (!data.hasOwnProperty(name)) {

			continue;

		}

		var value = data[name];

		buffer.push(encodeURIComponent(name) + "="
				+ encodeURIComponent((value == null) ? "" : value));

	}

	// Serialize the buffer and clean it up for transportation.
	var source = buffer.join("&").replace(/%20/g, "+");

	return (source);

}
